package com.helasasa.maxfunds.lineofcredit.cashloan.cameroon.screen.proof.subview

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import android.view.View


class ProofCameroonOcrFrameMask(context: Context, attrs: AttributeSet? = null) : View(context, attrs) {

    private val lineLength = 30F.dpToPx()
    private val lineWidth = 3F.dpToPx()

    private val linePaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        color = 0xFFFFFFFF.toInt()
        strokeWidth = lineWidth
        style = Paint.Style.STROKE
        strokeCap = Paint.Cap.ROUND
        strokeJoin = Paint.Join.ROUND
    }

    private val path = Path()

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

        // 左上角
        val point1X = lineWidth
        val point1Y = lineLength + lineWidth
        val point2X = lineWidth
        val point2Y = lineWidth
        val point3X = lineLength + lineWidth
        val point3Y = lineWidth

        // 左下角
        val point4X = lineLength + lineWidth
        val point4Y = height - lineWidth
        val point5X = lineWidth
        val point5Y = height - lineWidth
        val point6X = lineWidth
        val point6Y = height - lineWidth - lineLength

        // 右上角
        val point7X = width - lineWidth - lineLength
        val point7Y = lineWidth
        val point8X = width - lineWidth
        val point8Y = lineWidth
        val point9X = width - lineWidth
        val point9Y = lineLength + lineWidth

        // 右下角
        val point10X = width - lineWidth
        val point10Y = height - lineWidth - lineLength
        val point11X = width - lineWidth
        val point11Y = height - lineWidth
        val point12X = width - lineWidth - lineLength
        val point12Y = height - lineWidth

        // 绘制第一条线
        path.reset()
        path.moveTo(point1X, point1Y)
        path.lineTo(point2X, point2Y)
        path.lineTo(point3X, point3Y)
        canvas.drawPath(path, linePaint)

        // 绘制第二条线
        path.reset()
        path.moveTo(point4X, point4Y)
        path.lineTo(point5X, point5Y)
        path.lineTo(point6X, point6Y)
        canvas.drawPath(path, linePaint)

        // 绘制第三条线
        path.reset()
        path.moveTo(point7X, point7Y)
        path.lineTo(point8X, point8Y)
        path.lineTo(point9X, point9Y)
        canvas.drawPath(path, linePaint)

        // 绘制第四条线
        path.reset()
        path.moveTo(point10X, point10Y)
        path.lineTo(point11X, point11Y)
        path.lineTo(point12X, point12Y)
        canvas.drawPath(path, linePaint)

        path.reset()
    }

    // 将 dp 转换为像素
    private fun Float.dpToPx(): Float {
        return this * context.resources.displayMetrics.density
    }
}